function showImg(input) {
    const file = input.files[0];
    const url = window.URL.createObjectURL(file);
    console.log(url);
    document.getElementById('myimage').src = url;
}

// function F_Submit() {
$.F_Submit = function (e) {
    const file = $.find("input")[0].files[0];
    if (!file) {
        alert("请选择一个文件!");
        return;
    }

    const reader = new FileReader();
    reader.readAsDataURL(file);
    var data_64;
    reader.onloadend = function (e) {
        data_64 = e.target.result;
        console.log(data_64);
        var uid = $("#uid").val();
        var group = $("#groupId").val();
        var userInfo = $("#userInfo").val();
        $.ajax({
            url: "/aip/add",
            type: 'POST',
            data: {image: data_64, uid: uid, userInfo: userInfo, groupId: group},
            dataType: "json",
            success: function (data) {
                alert("错误码: " + data.error_code + "\n" + data.error_msg);
            }
        });
    };

};

$(document).ready(function (e) {
    $("#btn_upload").click(function () {
        var uid = $("#uid").val();
        console.log(uid);
        $.F_Submit();
    });
    $("#btn_delete").click(function () {
        var uid = $("#uid").val();
        var group = $("#group").val();
        $.ajax({
            url: "/aip/delete",
            type: 'post',
            data: {uid: uid, group: group},
            dataType: 'json',
            success: function (data) {
                if (data.error_msg != null) {
                    alert(data.error_msg);
                } else {
                    alert("删除成功");
                }

            }
        });
    })
    getGroup();
});

function getGroup(group) {
    $.ajax({
        url: "/aip/find",
        type: 'post',
        data: {group: group},
        dataType: 'json',
        success: function (data) {
            if (group == null) {
                var tbody = "";
                for (var i = 0; i < data.result_num; i++) {
                    var tr = "<tr>";
                    tr += "<td>" + (i + 1) + "</td>";
                    tr += "<td><a href='#'>" + data.result[i] + "</a></td>";
                    tr += "</tr>";
                    tbody += tr;
                }
                $('#Table thead').empty();
                var thead = "<tr>" + "<th width='50'>序号</th>" + "<th width='150'>组名</th>" + "</tr>";
                $('#Table thead').append(thead);
                $('#Table tbody').empty();
                $('#Table').children('tbody').append(tbody);
                $("#Table a").each(function (i) {
                    $(this).click(function () {
                        var group = $(this).text();
                        getGroup(group);
                    })
                })
            } else {
                var tbody = "";
                $.each(data.result, function (index, el) {
                    var tr = "<tr>";
                    tr += "<td>" + (index + 1) + "</td>";
                    tr += "<td>" + el.uid + "</td>";
                    tr += "<td>" + el.user_info + "</td>";
                    tr += "</tr>";
                    tbody += tr;
                })
                $('#Table thead').empty();
                var thead = "<tr>" + "<th width='50'>序号</th>" + "<th width='150'>用户名</th>" + "<th width='150'>用户信息</th>" + "</tr>";
                $('#Table thead').append(thead);
                $('#Table tbody').empty();
                $('#Table').children('tbody').append(tbody);
            }

        }
    });
}